package com.example.jdbc_demo.uitl;

import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBC_demo {
    //加载mysql5的数据库驱动
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //连接数据库
    public static java.sql.Connection getConnection() {
        try {
            return DriverManager.getConnection("jdbc:mysql://172.18.92.57:3306/javaweb", "rjgc21", "rjgc21");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //得到SQL查询语句对象
    public static java.sql.Statement getStatement() {
        try {
            return getConnection().createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询user表的数据
    public static void main(String[] args) {
        try {
            java.sql.Statement statement = getStatement();
            java.sql.ResultSet resultSet = statement.executeQuery("select * from user");
            //遍历结果集，输出到控制台
            while (resultSet.next()) {
                System.out.println(resultSet.getString("username"));
                System.out.println(resultSet.getString("password"));
            }
            //释放资源
            resultSet.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

